ele=null
var isAlt = false;
var isq = false;
var isw = false;
var isr =false;



document.onkeyup=function(e) {
    if(e.which == 18) {isAlt=false;}
    if(e.which == 81) {isq=false;}
    if(e.which == 87) {isw=false;}
    if(e.which == 82) {isr=false;}



}
document.onkeydown=function(e){
    if(e.which == 18) isAlt=true;
    if(e.which == 81) {isq=true;}
    if(e.which == 87) {isw=true;}
    if(e.which == 82) {isr=true;}
    if(e.which == 67 && isAlt == true) {
      v=window.getSelection().toString()
      if(v!=''){
        chrome.runtime.sendMessage({ cmd: 'c', value: v }, function(response) {
          console.log('收到来自后台的回复：' + response);
        });
           return false;

      }
      
    }
    if(e.which == 86 && isAlt == true) {
      chrome.runtime.sendMessage({ cmd: 'v' }, function(response) {
        // console.log(response)
        if(response!=''){
          console.log('收到来自后台的回复：' + response);
          
         
  
        }
        return false;
        });
        
 }
 else 
 if(e.which == 83 && isAlt == true) {
  chrome.runtime.sendMessage({ cmd: 's' }, function(response) {
    // console.log(response)
    if(response!=''){
    
      console.log('收到来自后台的回复：' + response);
    
     
    }
    return false;
    });
    
}
else 
if(e.which == 78 && isAlt == true) {
 chrome.runtime.sendMessage({ cmd: 'n' }, function(response) {
   // console.log(response)
   if(response!=''){
   
   
     console.log('收到来自后台的回复：' + response);
   
    
   }
   return false;
   });
   
}
else 
if(e.which == 84 && isAlt == true) {
 chrome.runtime.sendMessage({ cmd: 't' }, function(response) {
   // console.log(response)
   if(response!=''){

   
     console.log('收到来自后台的回复：' + response);
   
    
   }
   return false;
   });
   
}
else 
if(e.which == 80 && isAlt == true) {
  ele= document.activeElement
  st=ele.selectionStart;

  const event = document.createEvent('HTMLEvents')
    
   
   
      v1=window.getSelection().toString()
     
     if(ele.tagName=='INPUT' || ele.tagName=='TEXTAREA'){
       if(v1===''){
        ele.value=''
       }else{
        ele.value=ele.value.replace(v1,'')
       }
       
       
       
       event.initEvent('input', false, true)
        ele.dispatchEvent(event)
        event.initEvent('change', false, true)
        ele.dispatchEvent(event)
        event.initEvent('blur', false, true)
        ele.dispatchEvent(event)
     }else{
      if(v1===''){
        ele.innerHTML=''
       }else{
        ele.innerHTML=ele.innerHTML.replace(v1,'')
       }
       
     }
   
   
}
else 
if(((e.which>47 && e.which<58 ) ||(e.which>95 && e.which<106 )) && isAlt == true) {
  console.log(e.which)
  cc=''
  if(isq) cc+='q';
  if(isw) cc+='w';
  if(isr) cc+='r';
  if(cc===''){
    return 0;
  }
  nn=e.which
  if(nn>47 && nn<58 ){
    nn=nn-48
    if(nn==0) nn=10;
  }else
  if(nn>95 && nn<106 ){
    nn=nn-96
    if(nn==0) nn=10;
  }
 chrome.runtime.sendMessage({ cmd: cc,n:nn }, function(response) {
  //  console.log(response)
   if(response!=''){

   
     console.log('收到来自后台的回复：' + response);
   
    
   }
   return false;
   });
   
}
}

document.oncontextmenu=function (ev){
    var ev = ev || event;
    // ev.preventDefault();
    // console.log(111)
    // console.log(ev)
     ele = ev.toElement;
    console.log(ele)
    // chrome.runtime.sendMessage({greeting: ele}, function(response) {
    //     console.log('收到来自后台的回复：' + response);
    // });
    // chrome.storage.sync.set({youji: ele}, function() {
    //     chrome.storage.sync.get({youji: '1'}, function(items) {
    //         console.log(items.youji);
    //       });
    //     console.log('ok')
    // });
  
  }

  chrome.runtime.onMessage.addListener(function(request, sender, sendResponse)
{
  // console.log(request)
  // console.log(sender.tab ?"from a content script:" + sender.tab.url :"from the extension");
  
	if(request.cmd == 'test'){
        // console.log(ele)
        ele= document.activeElement
        st=ele.selectionStart;
      
        const event = document.createEvent('HTMLEvents')
          
         
          chrome.storage.sync.get({zhuijia: true}, function(items) {
            v1=window.getSelection().toString()
            v=''
            v2=''
           if(ele.tagName=='INPUT' || ele.tagName=='TEXTAREA'){
             if(items.zhuijia){
               v3=ele.value.replace(v1,'')
               v=v3.substring(0,st)
               v2=v3.substring(st)
             }
             
             ele.value=v+request.value+v2
             event.initEvent('input', false, true)
              ele.dispatchEvent(event)
              event.initEvent('change', false, true)
              ele.dispatchEvent(event)
              event.initEvent('blur', false, true)
              ele.dispatchEvent(event)
           }else{
             if(items.zhuijia){
              v3=ele.innerHTML.replace(v1,'')
              v=v3.substring(0,st)
              v2=v3.substring(st)
               
             }
             ele.innerHTML=v+request.value+v2
           }
         });
          
        
        //    chrome.storage.sync.get({zhuijia: true}, function(items) {
        //      v=''
        //     if(ele.tagName=='INPUT'){
        //       event = document.createEvent('HTMLEvents')
              
        //       if(items.zhuijia){
        //         v=ele.value
        //       }
              
        //       ele.value=v+request.value
        //       event.initEvent('input', false, true)
        //       ele.dispatchEvent(event)
        //       event.initEvent('change', false, true)
        //       ele.dispatchEvent(event)
        //       event.initEvent('blur', false, true)
        //       ele.dispatchEvent(event)
        //     }else{
        //       if(items.zhuijia){
        //         v=ele.innerHTML
        //       }
        //       ele.innerHTML=v+request.value
        //     }
        //   });
        
        
        
        sendResponse('我收到了你的消息！');
    } 
	
});
